{{- $name := .Chart.Name }}
{{- $globalAnnotationsCheck := include "global.annotations.check" . }}
{{- $globalContEnvsCheck := include "global.containers.envs.check" . }}
{{- $globalContExtraCheck := include "global.containers.extraEnvs.check" . }}
{{- $globalContRegistry := include "global.containers.registry" . }}
{{- $globalInitEnvsCheck := include "global.initContainers.envs.check" . }}
{{- $globalInitExtraCheck := include "global.initContainers.extraEnvs.check" . }}
{{- $globalInitRegistry := include "global.initContainers.registry" . }}
{{- range $index, $val := .Values.items }}
{{- if eq $val.type "deployment" }}
apiVersion: apps/v1
kind: Deployment
metadata:
{{- if or $val.annotations $globalAnnotationsCheck }}
  annotations:
{{- if $val.annotations }}
{{- toYaml $val.annotations | nindent 4 }}
{{- end }}
{{- if $globalAnnotationsCheck }}
{{- range $Index, $Val := $.Values.items }}
{{- if eq $Val.type "global" }}
{{- toYaml $Val.annotations | nindent 4 }}
{{- end }}{{- end }}{{- end }}
{{- end }}
  name: {{ $val.name | default (print $name "-" $val.type "-" $index) }}
  labels:
    app: {{ $name }}
    tier: {{ $val.name | default (print $name "-" $val.type) }}
spec:
  {{- if not (and $val.autoscaling $val.autoscaling.enabled) }}
  replicas: {{ $val.replicas| default 0 }}
  {{- end }}
  {{- if $val.strategy }}
  strategy:
    {{- toYaml $val.strategy | nindent 4 }}
  {{- else }}
  strategy:
    type: {{ print "RollingUpdate" }}
  {{- end }}
  selector:
    matchLabels:
      app: {{ $name }}
      tier: {{ $val.name | default (print $name "-" $val.type "-" $index) }}
  template:
    metadata:
      labels:
        app: {{ $name }}
        tier: {{ $val.name | default (print $name "-" $val.type "-" $index) }}
    spec:
      {{- if and $val.imagePullSecrets $val.imagePullSecrets.enabled }}
      imagePullSecrets:
        - name: {{ $val.imagePullSecrets.name | default (print $name "-dockerconfig") }}
      {{- end }}
      restartPolicy: {{ $val.restartPolicy | default (print "OnFailure") }}
      {{- with $val.volumes }}
      volumes:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if $val.initContainers }}
      initContainers:
        {{- range $indexInitContainers, $valInitContainers := $val.initContainers }}
        - name: {{ $valInitContainers.name | default (print $name "-init-" $indexInitContainers) }}
          {{- if not $globalInitRegistry }}
          image: {{ print (default "registry.hub.docker.com" $valInitContainers.registry) "/"
          $valInitContainers.image ":" (default "latest" $valInitContainers.imageTag ) }}
          {{- else }}
          image: {{ print $globalInitRegistry "/"
          $valInitContainers.image ":" (default "latest" $valInitContainers.imageTag ) }}
          {{- end }}
          {{- with $valInitContainers.command }}
          command:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $valInitContainers.args }}
          args:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $valInitContainers.volumeMounts }}
          volumeMounts:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $valInitContainers.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if $valInitContainers.imagePullPolicy }}
          imagePullPolicy: {{ $valInitContainers.imagePullPolicy }}
          {{- end }}
          {{- if not (or $valInitContainers.envs $globalInitEnvsCheck) }}
          {{- if or $valInitContainers.configMap $valInitContainers.secret $valInitContainers.extraEnvs $globalInitExtraCheck }}
          {{- if or $valInitContainers.configMap $valInitContainers.secret }}
          {{- if or $valInitContainers.configMap.enabled $valInitContainers.secret.enabled }}
          envFrom:
            {{- if $valInitContainers.configMap.enabled }}
            - configMapRef:
                name: {{ $valInitContainers.configMap.name | default (print $name "-config") }}
            {{- end }}
            {{- if $valInitContainers.secret.enabled }}
            - secretRef:
                name: {{ $valInitContainers.secret.name | default (print $name "-secret") }}
            {{- end }}
          {{- end }}{{- end }}
          {{- if or $valInitContainers.extraEnvs $globalInitExtraCheck }}
          env:
            {{- if $valInitContainers.extraEnvs }}
            {{- range $valInitContainers.extraEnvs }}{{- if .value }}
            - name: {{ .name }}
              value: {{ .value | quote }}
            {{- end }}{{- end }}{{- end }}
            {{- range $Ind, $Val := $.Values.items }}
            {{- if eq $Val.type "global" }}
            {{- range $_, $containers := $Val.containers }}
            {{- range $__, $containersExtraEnvs := $containers.extraEnvs }}{{- if $containersExtraEnvs.value }}
            - name: {{ $containersExtraEnvs.name }}
              value: {{ $containersExtraEnvs.value | quote }}
            {{- end }}{{- end }}{{- end }}
            {{- end }}{{- end }}
          {{- end }}
          {{- end }}
          {{- else }}
          env:
            {{- if and (not $globalInitEnvsCheck) $valInitContainers.envs }}
            {{- range $valInitContainers.envs }}{{- if .value }}
            - name: {{ .name }}
              value: {{ .value | quote }}
            {{- end }}{{- end }}{{- end }}
            {{- if $globalInitEnvsCheck }}
            {{- range $Ind, $Val := $.Values.items }}
            {{- if eq $Val.type "global" }}
            {{- range $_, $containers := $Val.containers }}
            {{- range $__, $containersEnvs := $containers.envs }}{{- if $containersEnvs.value }}
            - name: {{ $containersEnvs.name }}
              value: {{ $containersEnvs.value | quote }}
            {{- end }}{{- end }}{{- end }}
            {{- end }}{{- end }}
            {{- end }}
          {{- end }}
        {{- end }}
      {{- end }}
      {{- if $val.containers }}
      containers:
        {{- range $indexContainers, $valContainers := $val.containers }}
        - name: {{ $valContainers.name | default (print $name "-cont-" $indexContainers) }}
          {{- if not $globalContRegistry }}
          image: {{ print (default "registry.hub.docker.com" $valContainers.registry) "/"
          $valContainers.image ":" (default "latest" $valContainers.imageTag ) }}
          {{- else }}
          image: {{ print $globalContRegistry "/"
          $valContainers.image ":" (default "latest" $valContainers.imageTag ) }}
          {{- end }}
          {{- with $valContainers.command }}
          command:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $valContainers.args }}
          args:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $valContainers.volumeMounts }}
          volumeMounts:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $valContainers.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $valContainers.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $valContainers.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if $valContainers.ports }}
          ports:
            {{- range $_, $ports := $valContainers.ports }}
              {{- if .containerPort }}
            - containerPort: {{ .containerPort }}
              {{- if .name }}
              name: {{ .name }}
              {{- end }}
              {{- if .hostIP }}
              hostIP: {{ .hostIP }}
              {{- end }}
              {{- if .hostPort }}
              hostPort: {{ .hostPort }}
              {{- end }}
              {{- if .protocol }}
              protocol: {{ .protocol }}
              {{- end }}
              {{- end }}
            {{- end }}
          {{- end }}
          {{- if $valContainers.imagePullPolicy }}
          imagePullPolicy: {{ $valContainers.imagePullPolicy }}
          {{- end }}
          {{- if not (or $valContainers.envs $globalContEnvsCheck) }}
          {{- if or $valContainers.configMap $valContainers.secret $valContainers.extraEnvs $globalContExtraCheck }}
          {{- if or $valContainers.configMap $valContainers.secret }}
          {{- if or $valContainers.configMap.enabled $valContainers.secret.enabled }}
          envFrom:
            {{- if $valContainers.configMap.enabled }}
            - configMapRef:
                name: {{ $valContainers.configMap.name | default (print $name "-config") }}
            {{- end }}
            {{- if $valContainers.secret.enabled }}
            - secretRef:
                name: {{ $valContainers.secret.name | default (print $name "-secret") }}
            {{- end }}
          {{- end }}{{- end }}
          {{- if or $valContainers.extraEnvs $globalContExtraCheck }}
          env:
            {{- if $valContainers.extraEnvs }}
            {{- range $valContainers.extraEnvs }}{{- if .value }}
            - name: {{ .name }}
              value: {{ .value | quote }}
            {{- end }}{{- end }}{{- end }}
            {{- range $Ind, $Val := $.Values.items }}
            {{- if eq $Val.type "global" }}
            {{- range $_, $containers := $Val.containers }}
            {{- range $__, $containersExtraEnvs := $containers.extraEnvs }}{{- if $containersExtraEnvs.value }}
            - name: {{ $containersExtraEnvs.name }}
              value: {{ $containersExtraEnvs.value | quote }}
            {{- end }}{{- end }}{{- end }}
            {{- end }}{{- end }}
          {{- end }}
          {{- end }}
          {{- else }}
          env:
            {{- if and (not $globalContEnvsCheck) $valContainers.envs }}
            {{- range $valContainers.envs }}{{- if .value }}
            - name: {{ .name }}
              value: {{ .value | quote }}
            {{- end }}{{- end }}{{- end }}
            {{- if $globalContEnvsCheck }}
            {{- range $Ind, $Val := $.Values.items }}
            {{- if eq $Val.type "global" }}
            {{- range $_, $containers := $Val.containers }}
            {{- range $__, $containersEnvs := $containers.envs }}{{- if $containersEnvs.value }}
            - name: {{ $containersEnvs.name }}
              value: {{ $containersEnvs.value | quote }}
            {{- end }}{{- end }}{{- end }}
            {{- end }}{{- end }}
            {{- end }}
          {{- end }}
        {{- end }}
      {{- end }}
{{ print "---" }}
{{- end }}{{- end }}